package com.bjsc.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bjsc.entity.model.BetRecord;
import com.bjsc.entity.model.Player;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author create by VIC.Huang
 * @date 2018/2/25
 */
@Mapper
public interface PlayerMapper {

    @Select("<script>" +
            "select tu.id playerId, tu.nick_name playerName, tu.create_time registerTime, " +
            " tua.asset balance, tu.fromuid fromPlayerId, " +
            " (case when tu.`disable` = 1 then '冻结' else '正常' end) `status`, " +
            " (case when tu.system = 'YES' then '系统' else '普通' end) system, " +
            " (select nick_name from t_user where id = tu.fromuid) fromPlayerName " +
            "from t_user tu " +
            "join t_user_asset tua on tu.id = tua.user_id " +
            "<if test=\"search != null\">and (tu.id = #{search} or tu.nick_name like CONCAT('%',#{search,jdbcType=VARCHAR},'%'))</if>" +
            "order by tu.id desc" +
            "</script>")
    List<Map<Object, Object>> selectPlayerList(Page<Map<Object, Object>> page, @Param("search") String search);

    @Select("select db_game_title gameTitle, bet_num betNum, amount, per_price perPrice, order_no orderNo," +
            " issue_no issueNo, open_code openCode, win_status winStatus, win_price winPrice, create_time createTime" +
            " from t_db_bet_detail where user_id = #{userId} order by id desc")
    List<BetRecord> selectBetRecords(Page<Map<Object, Object>> page, @Param("userId") Long playerId);

    @Select("select id, create_time createTime, money, handle_status handleStatus, qrcode from t_withdraw_record where user_id = #{userId} order by id desc")
    List<Map<Object,Object>> selectWithdrawRecords(Page<Map<Object, Object>> page, @Param("userId") Long playerId);

    @Select("select create_time createTime, money from t_spend_record where user_id = #{userId} and type = 'RECHARGE' order by id desc")
    List<Map<Object,Object>> selectRechargeRecords(Page<Map<Object, Object>> page, @Param("userId") Long playerId);

    @Select("select sum(money) from t_spend_record where user_id = #{userId} and type = 'RECHARGE'")
    BigDecimal sumRecharge(@Param("userId") Long playerId);

    @Select("select sum(money) from t_withdraw_record where user_id = #{userId} and handle_status = 'YES'")
    BigDecimal sumWithdraw(@Param("userId")Long playerId);

    @Select("select sum(money) from t_commission_record where user_id = #{userId}")
    BigDecimal sumCommission(@Param("userId")Long playerId);

    @Select("select sum(money) from t_payback_record where user_id = #{userId}")
    BigDecimal sumPayback(@Param("userId")Long playerId);

    @Select("select asset+frozen_asset from t_user_asset where user_id = #{userId}")
    BigDecimal getUserAsset(@Param("userId")Long playerId);

    @Select("select tcr.from_user_id playerId, tcr.from_user_name playerName, sum(tcr.money) totalCommission, tu.create_time registerTime" +
            " from t_commission_record tcr" +
            " join t_user tu on tcr.from_user_id = tu.id" +
            " where tcr.user_id = #{userId} group by tcr.from_user_id")
    List<Map<Object,Object>> selectInvitePlayers(Page<Map<Object, Object>> page, Long playerId);
}
